(function () {
  /**
   * @param {Window} w 
   * @returns {Document[]}
   */
  function list_documents(w = window, ret = []) {
    ret.push(w.document);
    for (const f of w.document.getElementsByTagName('iframe')) {
      ret.push(f.contentDocument)
      list_documents(f.contentWindow, ret)
    }
    return ret;
  }
  Object.assign(window, { list_documents })

  /**
   * @param {Window} w 
   * @returns {Window}
   */
  function root_window(w = window) {
    let ret = window
    while (ret.parent && ret.parent !== ret)
      ret = ret.parent;
    return ret;
  }
  Object.assign(window, { root_window })
  /**
   * @param {string} selectors
   * @param {Window} w 
   * @returns {Element|null}
   */
  function deep_query_selector(selectors) {
    const root = root_window();
    const documents = list_documents(root)
    for (const d of documents) {
      const r = d.querySelector(selectors);
      if (r) return r;
    }
    return null
  }
  /**
   * @param {string} selectors
   * @param {Window} w 
   * @returns {Element|null}
   */
  function deep_query_selector_all(selectors) {
    const ret = []
    const root = root_window();
    const documents = list_documents(root)
    for (const d of documents) {
      const rs = d.querySelectorAll(selectors);
      for (const r of rs) ret.push(r)
    }
    return ret
  }



  Object.assign(window, { deep_query_selector })


  function log_0(...args) {
    return console.log('[study-lover]', ...args)
  }
  function make_log(log_func, name) {
    return function (...args) {
      log_func('[' + name + ']', ...args)
    }
  }
  function is_chapter_item_finish(log_1, item) {
    const log_2 = make_log(log_1, 'is_chapter_item_finish')
    if (!item) log_2("[is_chapter_item_finish] item got " + item)
    return !!item.querySelector('.course-ware-item-right>img')
  }
  function is_section_item_finish(log_1, item) {
    const log_2 = make_log(log_1, 'is_section_item_finish')
    if (!item) log_2("[is_section_item_finish] item got " + item)
    return !!item.querySelector('.section-finish')
  }

  function get_list() {
    const log_1 = make_log(log_0, 'get_list')
    const level_0_list = Array.from(deep_query_selector_all('.beisen-elearning-common-left-catolog__left-stage-expand'))
    for (const level_0 of level_0_list)
      if (!level_0.querySelector('.beisen-elearning-common-left-catolog__left-stage-expand-content'))
        level_0.querySelector('.beisen-elearning-common-left-catolog__left-stage-expand-title')?.click()

    const chapter_items = Array.from(deep_query_selector_all('.chapter-expand-container-header')).filter(i => !is_chapter_item_finish(log_1, i))
    return chapter_items
  }

  function consume_list_item() {
    const log_1 = make_log(log_0, 'consume_list_item')
    log_1("begin")
    const list_item = get_list().shift();
    if (list_item) {
      log_0("即将学习", list_item.innerText)
      list_item.click();
      click_play_btn(10)
    } else {
      log_1('list empty, all done.');
    }
    log_1("end")
  }

  function click_play_btn(times) {
    const log_1 = make_log(log_0, 'click_play_btn')
    log_1("begin")
    const play_btn = deep_query_selector('.xgplayer-start');
    if (!play_btn) {
      if (times) {
        setTimeout(() => click_play_btn(--times), 1000)
        log_1("'.xgplayer-start' not found, will retry after 1sec, remain: " + times + " times")
      } else {
        log_1("'.xgplayer-start' not found, skin and call consume_list_item")
      }
    } else {
      play_btn.click()
      listen_video_ended(200)
    }
    log_1("end")
  }

  function listen_video_ended(times) {
    const log_1 = make_log(log_0, 'listen_video_ended')
    log_1("begin")
    const video = deep_query_selector(".xgplayer video");
    if (!video) {
      if (times) {
        setTimeout(() => listen_video_ended(--times), 1000)
        log_1("'.video-player-container video' not found, will retry after 100msec, remain: " + times + " times")
      } else {
        log_1("'.video-player-container video' not foun, skin and call consume_list_item")
        consume_list_item()
      }
    } else {
      log_1('listening video ended')
      video.addEventListener('ended', () => {
        log_1('video ended, call consume_list_item');
        consume_list_item()
      })
    }
    log_1("end")
  }

  window.get_list = log_0
  window.get_list = get_list
  window.make_log = make_log
  window.consume_list_item = consume_list_item
  consume_list_item()
})()